package com.emm.yixun.website.listeners;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;

import com.emm.yixun.common.enums.ResponseEnums;
import com.emm.yixun.common.model.ActcommitLog;
import com.emm.yixun.common.model.BusinessAbnorma;
import com.emm.yixun.common.model.PriceScheme;
import com.emm.yixun.common.model.dto.ActcommitLogDto;
import com.emm.yixun.common.model.dto.BusinessAbnormaDto;
import com.emm.yixun.common.model.dto.NoticeEntity;
import com.emm.yixun.common.model.dto.PriceSchemeDto;
import com.emm.yixun.common.model.enums.AuditStatusEnum;
import com.emm.yixun.common.model.enums.BusinessAbnormaEnum;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.marketing.service.BusinessAbnormaServiceFacade;
import com.emm.yixun.marketing.service.PriceSchemeDetailServiceFacade;
import com.emm.yixun.marketing.service.PriceSchemeServiceFacade;
import com.emm.yixun.system.service.notice.pricescheme.PriceSchemeInvalidNoticeFacade;
import com.emm.yixun.website.common.utils.SpringWebContextUtil;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.service.ActivitiProcessServiceImpl;
import com.emm.yixun.website.service.system.log.ActCommitLogService;
/**
* 类描述： 价格方案无效审核事件监听
 */
public class PriceSchemeInvalidListeners implements Serializable, ExecutionListener{
	private static final long serialVersionUID = -5167931257350625288L;

	@Override
	public void notify(DelegateExecution execution) throws Exception {
		// TODO Auto-generated method stub
		String businessAbnormaId=execution.getProcessBusinessKey(); //业务ID
		String processInstanceId=execution.getProcessInstanceId();	//流程ID
		PriceSchemeServiceFacade priceSchemeServiceFacade=getPriceSchemeServiceFacade();
		BusinessAbnormaServiceFacade businessAbnormaServiceFacade=getBusinessAbnormaServiceFacade();
		BusinessAbnormaDto baDto = new BusinessAbnormaDto();
		baDto.setProcInstId(processInstanceId);
		baDto.setId(Long.valueOf(businessAbnormaId));
		baDto.setBusinessType(BusinessAbnormaEnum.BusinessAbnorma_10.getNo());
		ResponseList<BusinessAbnorma> businessAbnormaResponse = businessAbnormaServiceFacade.findByDto(baDto);
		if(!businessAbnormaResponse.isSuccess()){
			throw new Exception("价格方案无效审核记录不存在");
		}
		if(null == businessAbnormaResponse.getEntity() || businessAbnormaResponse.getEntity().size() == 0){
			throw new Exception("价格方案无效审核记录不存在");
		}
		BusinessAbnorma businessAbnorma = businessAbnormaResponse.getEntity().get(0);
		if(null==businessAbnorma){
			throw new Exception("价格方案无效审核记录不存在");
		}
		Response<PriceScheme> priceSchemeResponse=priceSchemeServiceFacade.findById(businessAbnorma.getBusinessId());
		if(!priceSchemeResponse.isSuccess()){
			throw new Exception("价格方案记录不存在");
		}
		PriceScheme priceScheme=priceSchemeResponse.getEntity();
		if(null==priceScheme){
			throw new Exception("价格方案记录不存在");
		}
		if(getActivitiProcessServiceImpl().isCheckPass(processInstanceId)){
			
			boolean flag = checkAuditCommit(businessAbnorma);
			if(!flag) {
				throw new Exception(ResponseEnums.RESPONSEENUMS_999999.getName());
			}
			
			//更新审核状态
			BusinessAbnormaDto updateDto=new BusinessAbnormaDto();
			updateDto.setId(businessAbnorma.getId());
			updateDto.setAuditStatus(AuditStatusEnum.SUCCESS.getNo());
			updateDto.setCheckDate(new Date());
			Response<Integer> responseBusinessAbnormaDto=businessAbnormaServiceFacade.updateByPrimaryKey(updateDto, true);
			if(!responseBusinessAbnormaDto.isSuccess()){
				throw new Exception("价格方案无效审核状态出错");
			}
			//更新价格方案状态
			PriceSchemeDto dto=new PriceSchemeDto();
			dto.setId(priceScheme.getId());
			if(ShiroUtil.getCurrentUser()!=null){
			    dto.setInvalidId(ShiroUtil.getCurrentUser().getId());
			}
			dto.setInvalidTime(new Date());
			dto.setStatus("20");
			Response<Integer> integerResponse=priceSchemeServiceFacade.updateByPrimaryKey(dto, true);
			if(!integerResponse.isSuccess()){
				throw new Exception("更新价格方案状态出错");
			}
			
			getPriceSchemeDetailServiceFacade().updateRoomStatusByGroupId(dto.getId());
			
			/**
			 * （价格方案无效审核通过）推送消息到app
			 */
			NoticeEntity noticeEntity = new NoticeEntity();
			noticeEntity.setNoticeBusinessId(businessAbnorma.getBusinessId());
			noticeEntity.setNoticeType("15");
			noticeEntity.setProcessInstanceId(processInstanceId);
			noticeEntity.setAudStatus("1");//审核通过，标记状态“1”在组装报备消息校验时用到
			getPriceSchemeInvalidNoticeFacade().createPriceSchemeInvalidNotice(noticeEntity);
		}else{
			
			boolean flag = checkAuditCommit(businessAbnorma);
			if(!flag) {
				throw new Exception(ResponseEnums.RESPONSEENUMS_999999.getName());
			}
			
			//更新审核状态
			BusinessAbnormaDto updateDto=new BusinessAbnormaDto();
			updateDto.setId(businessAbnorma.getId());
			updateDto.setAuditStatus(AuditStatusEnum.REJECT.getNo());
			updateDto.setCheckDate(new Date());
			Response<Integer> responseBusinessAbnormaDto=businessAbnormaServiceFacade.updateByPrimaryKey(updateDto, true);
			if(!responseBusinessAbnormaDto.isSuccess()){
				throw new Exception("更新延期签约审核状态出错");
			}
			
			/**
			 * （价格方案无效审核不通过）推送消息到app
			 */
			NoticeEntity noticeEntity = new NoticeEntity();
			noticeEntity.setNoticeBusinessId(businessAbnorma.getBusinessId());
			noticeEntity.setNoticeType("15");
			noticeEntity.setProcessInstanceId(processInstanceId);
			noticeEntity.setAudStatus("2");//审核通过，标记状态“2”在组装报备消息校验时用到
			getPriceSchemeInvalidNoticeFacade().createPriceSchemeInvalidNotice(noticeEntity);
		}
	}
	
	/**
	 * 前置判断,审核记录是否已经提交,防止重复提交
	 * 产生原因: 提交审核,后台处理中时,刷新页面再次提交审核,会重复
	 * true: 可继续执行
	 * false: 不可执行
	 */
	public boolean checkAuditCommit(BusinessAbnorma entity) {
		// 业务数据为null时,默认通过.为了不改变老业务,不影响流程正常行走
		if (null == entity) {
			return true;
		}
		return getActcommitLogService().checkActCommit(entity.getAuditStatus(), entity.getMerchantId(), entity.getId(),
				entity.getProcInstId(), "价格方案无效", "PC");
	}
	
	private ActCommitLogService getActcommitLogService(){
		return (ActCommitLogService)SpringWebContextUtil.getApplicationContext().getBean("actCommitLogService");
	}
	
	private PriceSchemeInvalidNoticeFacade getPriceSchemeInvalidNoticeFacade(){
		return (PriceSchemeInvalidNoticeFacade)SpringWebContextUtil.getApplicationContext().getBean("priceSchemeInvalidNoticeFacade");
	}
	
	private BusinessAbnormaServiceFacade getBusinessAbnormaServiceFacade(){
		return (BusinessAbnormaServiceFacade)SpringWebContextUtil.getApplicationContext().getBean("businessAbnormaServiceFacade");
	}
	
	private ActivitiProcessServiceImpl getActivitiProcessServiceImpl(){
		return (ActivitiProcessServiceImpl)SpringWebContextUtil.getApplicationContext().getBean("activitiProcessServiceImpl");
	}
	
	private PriceSchemeServiceFacade getPriceSchemeServiceFacade() {
		return (PriceSchemeServiceFacade)SpringWebContextUtil.getApplicationContext().getBean("priceSchemeServiceFacade");
	}
	
	private PriceSchemeDetailServiceFacade getPriceSchemeDetailServiceFacade() {
		return (PriceSchemeDetailServiceFacade)SpringWebContextUtil.getApplicationContext().getBean("priceSchemeDetailServiceFacade");
	}

}
